package com.example.shop_2302.address

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.base.param.Address
import com.example.base.param.ParamAddAddress
import com.example.shop_2302.R
import com.example.shop_2302.databinding.ActivityAddAddressBinding
import com.github.gzuliyujiang.wheelpicker.AddressPicker
import com.github.gzuliyujiang.wheelpicker.annotation.AddressMode

@Route(path = "/app/AddAddressActivity")
class AddAddressActivity : BaseActivity<ActivityAddAddressBinding,AddressViewModel>() {
    lateinit var address: Address
    override fun initView() {
        address = Address()
        //添加地址
        binding.addArea.setOnClickListener {
            val addressPicker = AddressPicker(this)//如果实在fragment中编写 requireActivity
            addressPicker.setTitle("请选择配送地址")
            addressPicker.setAddressMode(AddressMode.PROVINCE_CITY_COUNTY)
            addressPicker.setOnAddressPickedListener { province, city, county ->
                binding.addArea.text = "${province.name}${city.name}${county.name}"
                address.province = province.name
                address.city = city.name
                address.district = county.name
            }
            addressPicker.show()
        }
        //点击保存，实现保存地址
        binding.addSave.setOnClickListener {
            //请求后台
            val paramAddAddress = ParamAddAddress()
            paramAddAddress.detail = binding.addDetail.text.toString()
            paramAddAddress.phone = binding.addPhone.text.toString()
            paramAddAddress.realName = binding.addRealname.text.toString()
            paramAddAddress.isDefault = binding.addDefault.isChecked
            paramAddAddress.address =address
            viewModel.addAddress(paramAddAddress)
        }


    }

    override fun initData() {
        viewModel.addSuccess.observe(this){
            if(it.code==200){
                finish()
            }else{
                ToastUtils.showLong(it.message)
            }
        }
        viewModel.addFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

    override fun getViewModel(): Class<AddressViewModel> = AddressViewModel::class.java

    override fun getLayout(): Int = R.layout.activity_add_address

}